<template>
  <div class="login-container">
    <div class="login-box">
      <div class="logo">
        <svg viewBox="0 0 24 24" class="logo-icon">
          <path d="M12 2L2 7l10 5 10-5-10-5zM2 17l10 5 10-5M2 12l10 5 10-5"></path>
        </svg>
        <h1 class="logo-text">账号登录</h1>
      </div>

      <form @submit.prevent="handleLogin" class="login-form">
        <div class="input-wrapper">
          <i class="fa fa-user input-icon"></i>
          <input
            id="username"
            v-model="username"
            type="text"
            placeholder="请输入用户名"
            class="form-input"
            :class="{ 'input-error': errorMessage && !username.trim() }"
          />
        </div>

        <div class="input-wrapper">
          <i class="fa fa-lock input-icon"></i>
          <input
            id="password"
            v-model="password"
            type="password"
            placeholder="请输入密码"
            class="form-input"
            :class="{ 'input-error': errorMessage && !password.trim() }"
          />
        </div>

        <div class="remember-me">
          <input type="checkbox" id="remember" v-model="rememberMe">
          <label for="remember">记住密码</label>
        </div>

        <div v-if="errorMessage" class="error-message">
          <i class="fa fa-exclamation-circle"></i>
          <span>{{ errorMessage }}</span>
        </div>

        <button type="submit" class="login-button">
          <span v-if="!loading">登录</span>
          <span v-if="loading">登录中...</span>
        </button>
      </form>

      <div class="extra-links">
        <a href="#" class="link" @click.prevent="handleForgotPassword">忘记密码?</a>
        <a href="#" class="link" @click.prevent="handleRegister">注册账号</a>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      username: '',
      password: '',
      rememberMe: false,
      loading: false,
      errorMessage: ''
    }
  },
  methods: {
    handleLogin() {
      // 简单验证
      if (!this.username.trim() || !this.password.trim()) {
        this.errorMessage = '用户名和密码不能为空';
        return;
      }

      // 验证账号和密码
      if (this.username === 'user1' && this.password === '123456') {
        // 模拟登录过程
        this.loading = true;
        this.errorMessage = '';

        setTimeout(() => {
          // 模拟登录成功
          this.loading = false;
          this.$message.success('登录成功，即将跳转...');

          // 登录成功后将用户信息存储在 localStorage 中
          localStorage.setItem('user', JSON.stringify({ username: this.username }));

          // 登录成功后操作
          this.$router.push('/dashboard');
        }, 1500);
      } else {
        this.errorMessage = '用户名或密码错误';
      }
    },

    handleForgotPassword() {
      // 打开忘记密码页面或弹窗
      console.log('打开忘记密码页面');
      this.$router.push('/forgot-password');
    },

    handleRegister() {
      // 打开注册页面
      console.log('打开注册页面');
      this.$router.push('/register');
    }
  }
}
</script>

<style scoped>
/* 引入 Font Awesome 图标库 */
@import url('https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css');

.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f8fafc;
  background-image: 
    radial-gradient(circle at 25% 25%, rgba(16, 185, 129, 0.05) 0%, transparent 50%),
    radial-gradient(circle at 75% 75%, rgba(59, 130, 246, 0.05) 0%, transparent 50%);
}

.login-box {
  width: 100%;
  max-width: 400px;
  padding: 40px;
  background-color: #ffffff;
  border-radius: 12px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.05), 0 6px 6px rgba(0, 0, 0, 0.03);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.login-box:hover {
  transform: translateY(-5px);
  box-shadow: 0 15px 30px rgba(0, 0, 0, 0.1), 0 8px 10px rgba(0, 0, 0, 0.06);
}

.logo {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 30px;
}

.logo-icon {
  width: 60px;
  height: 60px;
  color: #10b981;
  margin-bottom: 15px;
}

.logo-text {
  font-size: 24px;
  font-weight: 600;
  color: #1e293b;
  margin: 0;
}

.login-form {
  display: flex;
  flex-direction: column;
}

.input-wrapper {
  position: relative;
  margin-bottom: 20px;
}

.input-icon {
  position: absolute;
  left: 14px;
  top: 50%;
  transform: translateY(-50%);
  color: #94a3b8;
  transition: color 0.3s ease;
  width: 20px;
  height: 20px;
}

.form-input:focus + .input-icon {
  color: #10b981;
}

.form-input {
  width: 100%;
  height: 48px;
  padding: 0 14px 0 44px;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  font-size: 16px;
  color: #334155;
  background-color: #ffffff;
  transition: border-color 0.3s ease, box-shadow 0.3s ease;
}

.form-input::placeholder {
  color: #94a3b8;
}

.form-input:focus {
  outline: none;
  border-color: #10b981;
  box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.2);
}

.input-error {
  border-color: #ef4444;
}

.input-error:focus {
  box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.2);
}

.remember-me {
  display: flex;
  align-items: center;
  margin-bottom: 25px;
  color: #64748b;
  font-size: 14px;
}

.remember-me input {
  margin-right: 8px;
  accent-color: #10b981;
}

.error-message {
  display: flex;
  align-items: center;
  color: #ef4444;
  background-color: #fee2e2;
  padding: 10px 14px;
  border-radius: 6px;
  margin-bottom: 20px;
  font-size: 14px;
  animation: fadeIn 0.3s ease;
}

.error-message i {
  margin-right: 8px;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(-10px); }
  to { opacity: 1; transform: translateY(0); }
}

.login-button {
  height: 48px;
  background-color: #10b981;
  color: #ffffff;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.3s ease, transform 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-button:hover {
  background-color: #059669;
}

.login-button:active {
  transform: scale(0.98);
}

.extra-links {
  display: flex;
  justify-content: space-between;
  margin-top: 25px;
}

.link {
  color: #3b82f6;
  text-decoration: none;
  font-size: 14px;
  transition: color 0.3s ease;
}

.link:hover {
  color: #2563eb;
  text-decoration: underline;
}
</style>